/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.DanhGiaGianHangDAO;
import com.miracleteam.miracle.entities.Danhgiagianhang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author DangKhoa
 */
public class DanhGiaGianHangDaoImpl implements DanhGiaGianHangDAO{
    Logger logger= Logger.getLogger(TinhThanhDAOImpl.class);
    public float LayTyLeTheoGiaCa(String maGH) {
        float kq=0;
         List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select dg from Danhgiagianhang dg  where dg.gianhang.maGianHang =:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        if(ds.isEmpty())
        {
            kq=0;
        }
        else
        {
            float sum=0;
            for(int i=0;i<ds.size();i++)
            {
                Danhgiagianhang item=(Danhgiagianhang)ds.get(i);
                sum+=item.getGiaCa();
            }
            kq=sum/ds.size();
        }

        return kq;
    }

    public float LayTyLeTheoThaiDoPhucVu(String maGH) {
        float kq=0;
         List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select dg from Danhgiagianhang dg  where dg.gianhang.maGianHang =:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        if(ds.isEmpty())
        {
            kq=0;
        }
        else
        {
            float sum=0;
            for(int i=0;i<ds.size();i++)
            {
                Danhgiagianhang item=(Danhgiagianhang)ds.get(i);
                sum+=item.getThaiDoPhucVu();
            }
            kq=sum/ds.size();
        }

        return kq;
    }

    public float LayTyLeTheoCheDoBaoHanh(String maGH) {
        float kq=0;
         List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select dg from Danhgiagianhang dg  where dg.gianhang.maGianHang =:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        if(ds.isEmpty())
        {
            kq=0;
        }
        else
        {
            float sum=0;
            for(int i=0;i<ds.size();i++)
            {
                Danhgiagianhang item=(Danhgiagianhang)ds.get(i);
                sum+=item.getCheDoBaoHanh();
            }
            kq=sum/ds.size();
        }

        return kq;
    }

    public float LayTyLeTheoLuonCoSPMoi(String maGH) {
       float kq=0;
         List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select dg from Danhgiagianhang dg  where dg.gianhang.maGianHang =:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        if(ds.isEmpty())
        {
            kq=0;
        }
        else
        {
            float sum=0;
            for(int i=0;i<ds.size();i++)
            {
                Danhgiagianhang item=(Danhgiagianhang)ds.get(i);
                sum+=item.getLuonCoMatHangMoi();
            }
            kq=sum/ds.size();
        }

        return kq;
    }

    public float LayTyLeTheoChatLuongSP(String maGH) {
        float kq=0;
         List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select dg from Danhgiagianhang dg  where dg.gianhang.maGianHang =:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        if(ds.isEmpty())
        {
            kq=0;
        }
        else
        {
            float sum=0;
            for(int i=0;i<ds.size();i++)
            {
                Danhgiagianhang item=(Danhgiagianhang)ds.get(i);
                sum+=item.getChatLuongSanPham();
            }
            kq=sum/ds.size();
        }

        return kq;
    }

    public float LayTyLeTheoNhanXetChung(String maGH) {
       float kq=0;
         List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select dg from Danhgiagianhang dg  where dg.gianhang.maGianHang =:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        if(ds.isEmpty())
        {
            kq=0;
        }
        else
        {
            float sum=0;
            for(int i=0;i<ds.size();i++)
            {
                Danhgiagianhang item=(Danhgiagianhang)ds.get(i);
                sum+=item.getNhanXetChung();
            }
            kq=sum/ds.size();
        }

        return kq;
    }
    public String PhatSinhMaKhachHang()
    {
        String kq="DG-";
        Date date = new Date();
        
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }
    public int ThemDanhGia(String maGH, Danhgiagianhang dg) {
         Session session = HibernateUtil.getSessionFactory().openSession();
         if(dg==null)
             return 0;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(dg);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
        return 1;
    }
     
}
